package com.qyhl.module_practice.order.type;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;
import com.orhanobut.hawk.Hawk;
import com.qyhl.module_practice.R;
import com.qyhl.module_practice.R2;
import com.qyhl.webtv.basiclib.base.BaseActivity;
import com.qyhl.webtv.basiclib.base.BaseIViewPresenter;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeListBean;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeOrderOptionBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ARouterPathConstant.PRACTICE_ORDER_TYPE)
public class PracticeOrderTypeActivity extends BaseActivity {
    @BindView(R2.id.title)
    TextView title;
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.search_btn)
    TextView searchBtn;
    @BindView(R2.id.search_txt)
    TextView searchTxt;


    private CommonAdapter commonAdapter;
    private LinearLayoutManager linearLayoutManager;

    private List<PracticeOrderOptionBean> oIndexList = new ArrayList<>();

    private List<PracticeListBean> sIndexList = new ArrayList<>();

    private List<PracticeOrderOptionBean> optionList = new ArrayList<>();

    private List<PracticeListBean> streetList = new ArrayList<>();

    private List<PracticeOrderOptionBean> oSearchList = new ArrayList<>();

    private List<PracticeListBean> sSearchList = new ArrayList<>();

    private int type;//0 类型  1 机构

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected int getContentView() {
        return R.layout.practice_activity_order_type;
    }

    @Override
    protected void setImmersionBar(ImmersionBar mImmersionBar) {
        setDefaultImmerBar();
    }

    @Override
    protected BaseIViewPresenter initBaseIViewPresenter() {
        return null;
    }

    @Override
    protected void init() {
        type = getIntent().getExtras().getInt("type", 0);
        if (type == 0) {
            optionList = (List<PracticeOrderOptionBean>) getIntent().getSerializableExtra("list");
            oIndexList.addAll(optionList);
        } else {
            streetList = Hawk.get("orderStreetList");
            sIndexList.addAll(streetList);
        }
        initView();

    }

    @Override
    protected void setListener() {
        setListeners();
    }

    private void initView() {
        recycleView.setLayoutManager(linearLayoutManager = new LinearLayoutManager(this));
        if (type == 0) {
            title.setText("选择类别");

            recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeOrderOptionBean>(this, R.layout.practice_item_order_type, oIndexList) {
                @Override
                protected void convert(ViewHolder holder, PracticeOrderOptionBean bean, int position) {
                    holder.setText(R.id.title, bean.getName());

                    if (bean.isChecked()) {
                        holder.setImageResource(R.id.select, R.drawable.practice_search_org_on);
                    } else {
                        holder.setImageResource(R.id.select, R.drawable.practice_search_org_off);
                    }
                }
            });

        } else {
            title.setText("选择机构");

            recycleView.setAdapter(commonAdapter = new CommonAdapter<PracticeListBean>(this, R.layout.practice_item_order_type, sIndexList) {
                @Override
                protected void convert(ViewHolder holder, PracticeListBean bean, int position) {
                    holder.setText(R.id.title, bean.getName());

                    if (bean.isChecked()) {
                        holder.setImageResource(R.id.select, R.drawable.practice_search_org_on);
                    } else {
                        holder.setImageResource(R.id.select, R.drawable.practice_search_org_off);
                    }
                }
            });

        }


    }

    private void setListeners() {
        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (type == 0) {
                    for (int i = 0; i < oIndexList.size(); i++) {
                        oIndexList.get(i).setChecked(i == position);
                    }

                    for (int i = 0; i < optionList.size(); i++) {
                        optionList.get(i).setChecked(oIndexList.get(position).getId() == optionList.get(i).getId());
                    }
                } else {
                    for (int i = 0; i < sIndexList.size(); i++) {
                        sIndexList.get(i).setChecked(i == position);
                    }

                    for (int i = 0; i < streetList.size(); i++) {
                        streetList.get(i).setChecked(sIndexList.get(position).getId() == streetList.get(i).getId());
                    }

                }
                commonAdapter.notifyDataSetChanged();

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        searchTxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {//按下搜索
                    String keyword = searchTxt.getText().toString();
                    if (StringUtils.isNotEmpty(keyword)) {
                        oSearchList.clear();
                        if (type == 0) {
                            for (int i = 0; i < optionList.size(); i++) {
                                if (optionList.get(i).getName().contains(keyword)) {
                                    oSearchList.add(optionList.get(i));
                                }
                            }
                            oIndexList.clear();
                            oIndexList.addAll(oSearchList);
                        } else {
                            sSearchList.clear();
                            for (int i = 0; i < streetList.size(); i++) {
                                if (streetList.get(i).getName().contains(keyword)) {
                                    sSearchList.add(streetList.get(i));
                                }
                            }

                            sIndexList.clear();
                            sIndexList.addAll(sSearchList);

                        }
                        commonAdapter.notifyDataSetChanged();
                    } else {
                        if (type == 0) {
                            oIndexList.clear();
                            oIndexList.addAll(optionList);
                        } else {
                            sIndexList.clear();
                            sIndexList.addAll(streetList);
                        }
                        commonAdapter.notifyDataSetChanged();
                    }
                    hideInput();
                }
                return false;//返回true，保留软键盘。false，隐藏软键盘
            }

        });
    }

    @OnClick({R2.id.back_btn, R2.id.commit_btn, R2.id.search_btn, R2.id.search_delete})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.back_btn) {
            finish();
        } else if (id == R.id.commit_btn) {
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt("type", type);
            if (type == 0) {
                bundle.putSerializable("list", (Serializable) optionList);
            } else {
                Hawk.put("orderStreetList", streetList);
            }
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        } else if (id == R.id.search_btn) {
            String keyword = searchTxt.getText().toString();
            if (StringUtils.isNotEmpty(keyword)) {
                if (type == 0) {
                    oSearchList.clear();
                    for (int i = 0; i < optionList.size(); i++) {
                        if (optionList.get(i).getName().contains(keyword)) {
                            oSearchList.add(optionList.get(i));
                        }
                    }
                    oIndexList.clear();
                    oIndexList.addAll(oSearchList);
                } else {
                    sSearchList.clear();
                    for (int i = 0; i < streetList.size(); i++) {
                        if (streetList.get(i).getName().contains(keyword)) {
                            sSearchList.add(streetList.get(i));
                        }
                    }

                    sIndexList.clear();
                    sIndexList.addAll(sSearchList);

                }
                commonAdapter.notifyDataSetChanged();
            } else {
                if (type == 0) {
                    oIndexList.clear();
                    oIndexList.addAll(optionList);
                } else {
                    sIndexList.clear();
                    sIndexList.addAll(streetList);
                }
                commonAdapter.notifyDataSetChanged();
            }
            hideInput();
        } else if (id == R.id.search_delete) {
            searchTxt.setText("");
            if (type == 0) {
                oIndexList.clear();
                oIndexList.addAll(optionList);
            } else {
                sIndexList.clear();
                sIndexList.addAll(streetList);
            }
            commonAdapter.notifyDataSetChanged();
        }
    }
}
